﻿@*<script>
        var getFixedColumnIndex = function (a) {
            for (var i = 0; i < a.length; i++) {
                if (a[i] == undefined) {
                    return i;
                }
            }
            return -1;
        };
        function delHtmlTag(str) {
            return str.replace(/<[^>]+>/g, "");//去掉所有的html标记
        }

        var getColumns = function (dgOptions, frozen) {

            //取目标列集合
            var fColumns = frozen == true ? (dgOptions.frozenColumns || [[]]) : (dgOptions.columns || [[]]).clone();

            //过滤checkbox列和hidden列
            var ddddd = fColumns.clone();
            for (var k = 0; k < fColumns.length; k++) {
                var tempLen = fColumns[k].length;
                for (var inK = 0; inK < tempLen; inK++) {
                    var needRemove = false, itemFc = fColumns[k][inK];
                    //checkbox列
                    if (itemFc.checkbox && itemFc.checkbox == true) { needRemove = true; }
                    //hidden列
                    if (!needRemove && itemFc.hidden && itemFc.hidden == true) { needRemove = true; }
                    //无title的列、title过滤html标签后为空的列
                    if (!needRemove && ($.string.isNullOrWhiteSpace(itemFc.title))) { needRemove = true; }
                    if (!needRemove) {
                        itemFc.title = delHtmlTag(itemFc.title);
                        if ($.string.isNullOrWhiteSpace(itemFc.title)) { needRemove = true; }
                    }

                    if (needRemove) { fColumns[k].removeAt(inK); tempLen--; inK--; }
                }
            }
            //计算列集合的总列数总行数
            //总行数 = fColumns.length
            //总列数 = fColumns[item].colspan之和中最大的
            var colspanCount = $.array.max($.array.map(fColumns, function (itemFc) { return $.array.sum(itemFc, function (item) { return item.colspan || 1; }) })),
                rowspanCount = fColumns.length;

            var lastColumns = [];
            //组建最终列集合的数组结构
            for (var i = 0; i < rowspanCount; i++) {
                lastColumns[i] = new Array(colspanCount);
            }

            var getFixedColumnIndex = function (a) {
                for (var i = 0; i < a.length; i++) {
                    if (a[i] == undefined) {
                        return i;
                    }
                }
                return -1;
            };
            //往最终列集合里填充数据
            for (var columIndex = 0; columIndex < fColumns.length; columIndex++) {
                fColumns[columIndex].forEach(function (itemFc, itemIndex) {
                    var fieldIndex = getFixedColumnIndex(lastColumns[columIndex]); //找到第一个未赋值的元素索引
                    if (fieldIndex >= 0) {
                        for (var c = fieldIndex; c < colspanCount ; c++) {
                            var tempCol = $.extend({}, itemFc, {});
                            if (tempCol.colspan == undefined) { tempCol.colspan = 1; }
                            if (tempCol.rowspan == undefined) { tempCol.rowspan = 1; }
                            if ((itemFc.colspan || 1) > 1) {
                                //若列是跨列的，则认为该列的field无效
                                delete tempCol.field;
                            }
                            lastColumns[columIndex][c] = tempCol;
                            if ((itemFc.rowspan || 1) > 1) {
                                for (var d = 1; d < itemFc.rowspan; d++) {
                                    if (columIndex + d <= rowspanCount) {
                                        lastColumns[columIndex + d][c] = tempCol;
                                    }
                                }
                            }
                            if ((itemFc.colspan || 1) > 1) {
                                for (var d = 1; d < itemFc.colspan; d++) {
                                    if (c + d <= colspanCount) {
                                        lastColumns[columIndex][c + d] = tempCol;
                                    }
                                }
                            }
                            break;
                        }
                    }
                });
            }

            return lastColumns;
        };



        var columns = [
            [
                { title: '测试', colspan: 3 }
            ],
            [
                { field: 'AreaName', title: '地区', width: 150, rowspan: 2 },
                { title: '性别', colspan: 2 }
            ],
            [
                { field: 'Sex1', title: '男', width: 90 },
                { field: 'Sex2', title: '女', width: 90 }
            ]
        ];

        //你最终需要的结果是：
        var test = [
            [
                { field: 'AreaName', title: '测试.地区', width: 150, rowspan: 2 },
                { field: 'Sex1', title: '测试.性别.男', width: 90 },
                { field: 'Sex2', title: '测试.性别.女', width: 90 }
            ]
        ];


        var options = {
            //frozenColumns
            columns: columns
        };

        var lastC = getColumns(options);

        console.log(lastC);

        //temp存 order（行号）、index（列号）、title、field
        var temp = [];
        $.each(lastC, function (index, item) {
            $.each(item, function (index2, column) {
                if (!$.array.contains(temp, index2, function (a, b) { return a.index == b; })) {
                    temp.push({ order: index, index: index2, title: column.title, field: column.field });
                } else {
                    var spe = $.array.last(temp, function (t) { return t.index == index2; });
                    if (spe.title.contains(".")) {
                        var arr_temp = spe.title.split(".");
                        if (!$.array.contains(arr_temp, column.title)) {
                            temp.push({ order: index, index: index2, field: column.field, title: spe.title + "." + column.title });
                        }
                        else {
                            temp.push({ order: index, index: index2, field: column.field, title: spe.title });
                        }
                    } else {
                        if (spe.title != column.title) {
                            temp.push({ order: index, index: index2, field: column.field, title: spe.title + "." + column.title });
                        }
                    }
                }
            });
        });
        console.log(temp);

        ////合并原始columns，返回新的数组
        //var result = [];
        //for (var i = 0; i < options.columns.length; i++) {
        //    $.each(options.columns[i], function (index, item) {
        //        if (i == 0) {
        //            result.push($.extend({}, item));
        //            return;
        //        }
        //        var the = $.array.first(temp, function (t) { return t.order == i && t.field == item.field; });
        //        if (the) {
        //            result.push($.extend({}, item, { title: the.title }));
        //        }
        //    });
        //}

        //合并到lastC中
        var result = [], lastIndex = lastC.length - 1;
        $.each(lastC[lastIndex], function (index, item) {
            var the = $.array.first(temp, function (t) { return t.order == lastIndex && t.field == item.field; });
            if (the) {
                result.push($.extend({}, item, { title: the.title }));
            }
        });


        console.log(result);

    </script>*@




<div id="p1" class="easyui-panel" style="padding:30px;" data-options="fit:true">
    <table id="dg"></table>
    <div id="toolbar">
        地区：<input type="text" id="name" name="name" />
        <a id="btnQuery" class="easyui-linkbutton">查询</a>
    </div>
</div>

<script>
    $(function () {
        $("#btnQuery").click(function () {
            $.ajax({
                url: "/Helper/test1.ashx",
                type:"post",
                success: function (res) {
                    console.log(res)
                },
                error: function (res, b ,c) {
                    console.log(res)
                    console.log(b)
                    console.log(c)
                }

            });

        });
    });
</script>